package com.four.seckill.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.four.common.utils.R;
import com.four.seckill.entity.CategoryEntity;
import com.four.seckill.service.CategoryService;
import com.four.seckill.util.OssUtils;
import com.four.seckill.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author: hxw
 * @Date: 2022/01/05/15:31
 * @Description
 */
@RestController
@RequestMapping("/seckill/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     * @return
     */
    @GetMapping("/list")
    public R list() {
        List<CategoryEntity> categoryEntity = categoryService.getList();
        return R.ok().put("cate",categoryEntity);
    }

    /**
     * 商品分类分页查
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/categoryList")
    public R categoryList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                          @RequestParam(value = "size",defaultValue = "3")Integer size,
                          CategoryVo vo
    ){
        IPage<CategoryEntity> categoryList=categoryService.categoryList(page,size,vo);
        return R.ok().put("categoryList",categoryList);
    }

    /**
     * 添加分类
     * @param category
     * @return
     */
    @PostMapping("/addCategory")
    public R addCategory(@RequestBody CategoryEntity category){
        categoryService.addCategory(category);
        return R.ok("添加成功");
    }

    /**
     * 删除分类
     * @param catId
     * @return
     */
    @DeleteMapping("/deleteById/{catId}")
    public R deleteById(@PathVariable("catId")Integer catId){
        categoryService.deleteById(catId);
        return R.ok("删除成功");
    }

    /**
     * 根据id查询商品分类
     * @param catId
     * @return
     */
    @GetMapping("/getCategoryById/{catId}")
    public R getCategoryById(@PathVariable("catId")Integer catId){
        CategoryEntity category=categoryService.getCategoryById(catId);
        return R.ok().put("category",category);
    }

    /**
     * 根据id修改商品分类
     * @param catId
     * @return
     */
    @PutMapping("/updateCategoryById/{catId}")
    public R updateCategoryById(@PathVariable("catId")Integer catId,@RequestBody CategoryEntity category){
        categoryService.updateCategoryById(catId,category);
        return R.ok("修改成功");
    }

    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("uploadCatIcon")
    public R uploadCatIcon(@RequestBody MultipartFile file){
        String c= OssUtils.uploadFile(file);
        return R.ok().put("url",c);
    }
}
